#!/bin/bash
# this is the boot-service init script for the sshterm resources
#
# openQRM Enterprise developed by openQRM Enterprise GmbH.
#
# All source code and content (c) Copyright 2014, openQRM Enterprise GmbH unless specifically noted otherwise.
#
# This source code is released under the GNU General Public License version 2, unless otherwise agreed with openQRM Enterprise GmbH.
# The latest version of this license can be found here: src/doc/LICENSE.txt
#
# By using this software, you acknowledge having read this license and agree to be bound thereby.
#
#           http://openqrm-enterprise.com
#
# Copyright 2014, openQRM Enterprise GmbH <info@openqrm-enterprise.com>
#

OPENQRM_SERVER_BASE_DIR=$(pushd $(dirname $0)/../../../../.. > /dev/null; echo $PWD; popd > /dev/null)
# for including the package functions
export OPENQRM_SOURCE_DIR="$OPENQRM_SERVER_BASE_DIR/openqrm/"
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/include/openqrm-package-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/sshterm/include/openqrm-plugin-sshterm-functions
. $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/sshterm/etc/openqrm-plugin-sshterm.conf
. $OPENQRM_RESOURCE_PARAMETER_FILE
# define wget to use with https
if [ "$openqrm_web_protocol" == "https" ]; then
	WGET_NO_CERT_CHECK="--no-check-certificate"
fi

# do not run on idle resources
if [ "$resource_image" == "idle" ]; then
	exit 0
fi
# also do not run before the real init of the rootfs
# if we have this file /etc/initrd-devices.conf we are still in the initrd
if [ -f /etc/initrd-devices.conf ]; then
	exit 0
fi


function sshterm_start() {
	echo "Starting the openQRM sshterm-plugin"
	local CURRENTDIR=`pwd`
	sshterm_stop 1>/dev/null 2>&1
	if ! which screen 1>/dev/null; then
		export FORCE_INSTALL=true
		openqrm_install_os_dependency screen
	fi
	if ! which openssl 1>/dev/null; then
		export FORCE_INSTALL=true
		openqrm_install_os_dependency openssl
	fi
	if [ -x /usr/bin/screen.real ]; then
		RUNSCREEN="/usr/bin/screen.real"
	else
		RUNSCREEN=`which screen`
	fi
	if [ -f /etc/debian_version ]; then
		if ! dpkg-query -s python-openssl 1>/dev/null 2>&1; then
			export FORCE_INSTALL=true
			openqrm_install_os_dependency python-openssl
		fi
	elif [ -f /etc/redhat-release ]; then
		if ! rpm -q pyOpenSSL 1>/dev/null 2>&1; then
			export FORCE_INSTALL=true
			openqrm_install_os_dependency pyOpenSSL
		fi
	elif [ -f /etc/SuSE-release ]; then
		if ! rpm -q python-openssl 1>/dev/null 2>&1; then
			export FORCE_INSTALL=true
			openqrm_install_os_dependency python-openssl
		fi
	fi
	# generate cert
	cd $OPENQRM_SERVER_BASE_DIR/openqrm/plugins/sshterm/ajaxterm/
	if [ ! -f /root/.ajaxterm/ajaxterm.pem ]; then
		umask 077
		mkdir -p /root/.ajaxterm/
		openssl req  -config "make_certificate.cfg" -keyout "/root/.ajaxterm/ajaxterm.pem" -newkey rsa:1024 -nodes -x509 -days 365 -out "/root/.ajaxterm/ajaxterm.pem"
	fi
	# patch SocketServer.py for python 2.7
	# webshell/python bug http://code.google.com/p/web-shell/issues/detail?id=67&q=2.7
	if [ -f /usr/lib/python2.7/SocketServer.py ]; then
		if [ ! -f /usr/lib/python2.7/SocketServer.py.openqrm-backup ]; then
			cp -f /usr/lib/python2.7/SocketServer.py /usr/lib/python2.7/SocketServer.py.openqrm-backup
		fi
		# pls preserve the line breaks in the following sed command
		if ! grep openQRM /usr/lib/python2.7/SocketServer.py 1>/dev/null; then
			sed -i -e 's/request.shutdown(socket.SHUT_WR)/request.shutdown(socket.SHUT_WR)\
		# openQRM: fix for webshell in combination with python 2\.7\
        except TypeError:\
            request.shutdown()\
/' /usr/lib/python2.7/SocketServer.py
		fi
	fi
	# apply css
	SCREEN_NAME=`date +%T%x | sed -e "s/://g" | sed -e "s#/##g"`
	$RUNSCREEN -dmS $SCREEN_NAME ./ajaxterm.py --host=0.0.0.0 --port=$OPENQRM_PLUGIN_WEBSHELL_PORT  --cert=/root/.ajaxterm/ajaxterm.pem
	cd $CURRENTDIR
	return 0
}


function sshterm_stop() {
	echo "Stopping the openQRM sshterm-plugin"
	AJAXTERM_PID=`ps ax | grep ajaxterm.py | grep -v grep | awk {' print $1 '}`
	for PID in $AJAXTERM_PID; do
		kill $PID
	done
}



case "$1" in
	start)
		sshterm_start
		;;
	stop)
		sshterm_stop
		;;
	restart)
		sshterm_stop
		sleep 1
		sshterm_start
		;;
	*)
		echo "Usage: $0 {start|stop|restart|createcert}"
		exit 1

esac
exit $?













